Skip to content


ai  101  pytorch  classification  nvidia  cuda  install  tensorrt  yolo  ardupilot  None  ros2  dds  micro ros  xrce  sitl  plugin  SITL  debug  rangefinder  pymavlink  mavros  gazebo  distance sensor  system_time  timesync  cmake  gtest  ctest  cpp  c++  format  fmt  multithreading  spdlog  camera  coordinate system  orb  matching  opencv  build  transformation  computer vision  homography  optical flow  of  trackers  cv  cyclonedds  eprosima  fastdds  simulation  config  ignition  bridge  sdf  tips  ign-transport  sensors  lidar  aptly  apt  encryption  pgp  docker  git  bundle  github  hooks  pre-commit  lxd  container  lxc  x11  profile  vscode  marpit  presentation  marp  markdown  mermaid  video  ffmpeg  gstreamer  cheat-sheet  sdp  v4l2loopback  gi  snippets  cheat Sheet  python  asyncio  future  click  cli  numpy  project  template  black  isort  docs  project document  docstrings  flake8  linter  git-hook  mypy  unittest  pytest  pylint  mock  iterator  generator  logging  tuple  namedtuple  typing  annotation  pyzmq  zmq  msgpack  action  namespace  remap  control2  ros2_control  gdb  qos  tag  plugins  msg  node  zero-copy  shm  tutorial  algorithm  calibration  diff  pid  dev  colcon  colcon_cd  rpi  arm  qemu  settings  behavior  plot  visualization  debugging  diagnostic  diagnostics  tutorials  gst  math  apm  rat_runtime_monitor  web  rosbridge  vue  binding  discovery  gazebo-classic  launch  spawn  cook  gps  imu  ray  gazebo_ros_ray_sensor  ultrsonic  range  ultrasonic  gazebo classic  wrench  effort  odom  ign  gz  xacro  ros_ign  diff_drive  odometry  joint_state  argument  OpaqueFunction  DeclareLaunchArgument  LaunchConfiguration  tmux  nav  slam  test  rclpy  executor  MultiThreadedExecutor  SingleThreadedExecutor  param  dynamic-reconfigure  service  client  setup.py  package.xml  parameter  parameters  custom  msgs  executers  pub  sub  rqt  rviz  rviz2  pose  marker  tf2  deb  package  setup  local_setup  rosdep  package manager  project settings  vcstool  cross-compiler  nano  texture  tmuxp  rootfs  embedded  zah  linux  rm  ubuntu  ip  ss  network  netstat  snap  deploy  ssh  systemd  mkdocs  extensions  socat  networking  serial  udp  tc  mtu  select  px4  robotics  kalman_filter  kalman  filter  control  todo  vscode-ext  json  yaml  schema  yocto  poky  world  gazebo_ros2_control  position_controller  effort_controller  velocity_controller  urdf  gazebo_ros_force  gazebo_ros_joint_state_publisher  robot_state_publisher  joint_state_publisher  projects  vrx  buoyancy 

GTest with cmake and VSCode


Table of Content

LAB objective#

  • Build cpp project with library using cmake
  • Create testing with GTest
  • Config VSCode

install#

sudo apt install libgtest-dev
├── app
│   ├── CMakeLists.txt
│   └── src
│       └── main.cpp
├── helper
│   ├── CMakeLists.txt
│   ├── include
│   │   └── helper
│   └── src
│       └── helper.cpp
├── tests
│    ├── CMakeLists.txt
│    └── test_helper.cpp
│
└── CMakeLists.txt

lib#

  • header file (definition)
  • cpp file (declaration)
  • cmake file
include/helper/helper.h
#pragma once

namespace helper{
    int add(int x, int y);
}
src/helper.cpp
#include "helper.h"

namespace helper
{
    int add(int x, int y)
    {
        return x + y;
    }
}
CMakeLists.txt
set (SOURCES
    src/helper.cpp
)
add_library(helper ${SOURCES})
target_include_directories(helper PUBLIC include/helper)

app#

  • cpp file
  • cmake file
app/src/main.cpp
#include <iostream>
#include <helper.h>

using namespace std;

int main(int argc, char const *argv[])
{
    cout << helper::add(1, 1) << endl;
    return 0;
}
CMakeLists.txt
add_executable(main src/main.cpp)
target_link_libraries(main helper)

test#

  • Test file example
  • cmake file
test_helper
#include <gtest/gtest.h>
#include <helper.h>

TEST(helper, add){
    EXPECT_EQ(0, helper::add(1, -1));
}
tests/CMakeList.txt
add_subdirectory(/usr/src/gtest
    ${PROJECT_BINARY_DIR}/gtest
)

include(CTest)

set(TEST_BINARY ${PROJECT_NAME}_test)
add_executable(${TEST_BINARY} test_helper.cpp)
target_link_libraries(${TEST_BINARY}
    helper
    gtest
    gtest_main
)

add_test(
    NAME ${TEST_BINARY}
    COMMAND ${EXECUTABLE_OUTPUT_PATH}/${TEST_BINARY}
)

root#

  • Root cmake file

root cmake must contain enable_testing cmake function for testing

tests/CMakeList.txt
cmake_minimum_required(VERSION 3.10)
project(demo)

enable_testing()

add_subdirectory(helper)
add_subdirectory(app)
add_subdirectory(tests)

usage#

command line#

ctest -VV
#
...
test 1
    Start 1: demo_test

1: Test command: /home/user/projects/gtest_tutorial/build/tests/demo_test
1: Test timeout computed to be: 1500
1: Running main() from /usr/src/gtest/src/gtest_main.cc
1: [==========] Running 1 test from 1 test suite.
1: [----------] Global test environment set-up.
1: [----------] 1 test from helper
1: [ RUN      ] helper.add
1: [       OK ] helper.add (0 ms)
1: [----------] 1 test from helper (0 ms total)
1: 
1: [----------] Global test environment tear-down
1: [==========] 1 test from 1 test suite ran. (0 ms total)
1: [  PASSED  ] 1 test.
1/1 Test #1: demo_test ........................   Passed    0.00 sec

VSCode#

VSCode marketplace

Usage#